<template>
  <a-modal
    :title="title"
    :width="780"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', { rules: [{ required: true, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>

        <a-form-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <Ueditor v-decorator="['content', { rules: [{ required: true, message: '内容不能为空' }] }]" />
        </a-form-item>

        <a-form-item label="老师" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['user_id', { rules: [{ required: true, message: '请选择1个文章分类' }] }]">
            <a-select-option v-for="(item, index) in teacherList" :key="index" :value="item.user_id">{{
              item.nick_name
            }}</a-select-option>
          </a-select>
        </a-form-item>
        // {{ record.type }}
        <a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择一个类型"
            v-decorator="[
              'type',
              { rules: [{ required: true, message: '请选择1个类型' }] },
              { initialValue: record.type },
            ]"
          >
            <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{ item.type }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            placeholder="请选择一个跳转路径"
            v-decorator="['url', { rules: [{ required: true, message: '请选择1个跳转路径' }] }]"
          >
            <a-select-option v-for="item in urlList" :key="item.id" :value="item.message">{{
              item.message
            }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/content/academy'
import { SelectImage, Ueditor } from '@/components'

export default {
  components: {
    SelectImage,
    Ueditor,
  },
  props: {
    // 类型列表
    typeList: {
      type: Array,
      required: true,
    },
    // 路由列表
    urlList: {
      type: Array,
      required: true,
    },
  },
  data() {
    return {
      // 对话框标题
      title: '编辑文章',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录ID
      user_id: null,
      // 当前记录详情
      record: {},
    }
  },
  methods: {
    // //**
    //  * 显示对话框
    //  */
    edit(record) {
      // 显示窗口
      this.visible = true
      // 当前记录ID
      this.record = record
      // 获取当前详情记录
      this.setFieldsValue()
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },
    /**
     * 设置默认值
     */
    setFieldsValue() {
      const {
        record,
        form: { setFieldsValue },
      } = this
      // console.log(record, 'record')
      this.$nextTick(() => {
        setFieldsValue(record)
      })
    },
    // 获取当前详情记录
    getDetail() {
      this.confirmLoading = true
      Api.detail({ id: this.id })
      console
        .log('id', Api.detail({ id: this.id }))
        .then((result) => {
          // 当前记录
          this.record = result.data.detail
          // 设置默认值
          this.setFieldsValue()
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
    /**
     * 设置默认值
     */
    setFieldsValue() {
      const {
        record,
        form: { setFieldsValue },
      } = this
      // console.log(record, 'record')
      this.$nextTick(() => {
        setFieldsValue(record)
      })
    },
    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },
    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.record = {}
      this.form.resetFields()
    },
    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.add(values)
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },
  },
  onFormSubmit(values) {
    this.confirmLoading = true
    Api.edit({ id: this.record.id, ...values })
      .then((result) => {
        // 显示成功
        this.$message.success(result.message, 1.5)
        // 关闭对话框事件
        this.handleCancel()
        // 通知父端组件提交完成了
        this.$emit('handleSubmit', values)
      })
      .finally((result) => {
        this.confirmLoading = false
      })
  },
}
</script>
